package com.zidian.teacher.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.zidian.teacher.R;
import com.zidian.teacher.bean.QuestionDetailListBean;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by GongCheng on 2016/10/19 0019.
 */

public class QuestionDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_CONTENT = 0;
    private static final int TYPE_OPTION = 1;
    private Context context;
    private List<QuestionDetailListBean> data;
    private int studentsNumber;

    public QuestionDetailAdapter(Context context, List<QuestionDetailListBean> data , int studentsNumber ) {
        this.context = context;
        this.data = data;
        this.studentsNumber = studentsNumber;
    }

    public void setData(List<QuestionDetailListBean> data) {
        this.data = data;
        notifyDataSetChanged();
    }
    /**
     * 设置TYPE
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        if (!data.get(position).isChild()) {
            return TYPE_CONTENT;
        } else {
            return TYPE_OPTION;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_CONTENT){
            View view = View.inflate(context,R.layout.item_question_detail_content,null);
            return new ContentViewHolder(view);
        }
        if (viewType == TYPE_OPTION) {
            View view = View.inflate(context, R.layout.item_question_detail_option,null);
            return new OptionViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ContentViewHolder) {
            ((ContentViewHolder) holder).title.setText(data.get(position).getItemName());
        }
        if (holder instanceof  OptionViewHolder) {
            ((OptionViewHolder) holder).optionName.setText(data.get(position).getItemName());
            ((OptionViewHolder) holder).optionNumber.setMax(studentsNumber);
            ((OptionViewHolder) holder).optionNumber.setProgress(data.get(position).getOptionNumber());
            ((OptionViewHolder) holder).optionTotal.setText(data.get(position).getOptionNumber()+"人");
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    static class ContentViewHolder extends RecyclerView.ViewHolder {
        TextView title;
        public ContentViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.question_detail_option);
        }
    }
    static class OptionViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.question_detail_option_name)
        TextView optionName;
        @Bind(R.id.question_detail_option_progress)
        ProgressBar optionNumber;
        @Bind(R.id.question_detail_total)
        TextView optionTotal;
        public OptionViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
